package com.lzy.webapi.facade;

import com.lzy.common.apis.ParamServiceApi;
import com.lzy.common.constants.CommonConstants;
import com.lzy.common.dtos.ParamBaseDto;
import com.lzy.framework.core.datasource.multiple.DataSource;
import com.lzy.framework.core.utils.DozerUtils;
import com.lzy.member.apis.MemberAccountServiceApi;
import com.lzy.member.dtos.MemberAccountDto;
import com.lzy.product.apis.OrderProductServiceApi;
import com.lzy.product.apis.OrderProjectRecordServiceApi;
import com.lzy.product.apis.ProductInfoServiceApi;
import com.lzy.product.dtos.OrderProductDto;
import com.lzy.product.dtos.OrderProjectRecordDto;
import com.lzy.product.dtos.ProductInfoDto;
import com.lzy.webapi.response.MemberStockDetailResponse;
import com.lzy.webapi.response.MemberStockResponse;
import com.lzy.webapi.response.StockResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class MemberFacade {

    @Autowired
    private MemberAccountServiceApi memberAccountServiceApi;

    @Autowired
    OrderProjectRecordServiceApi orderProjectRecordServiceApi;

    @Autowired
    OrderProductServiceApi orderProductServiceApi;

    @Autowired
    ParamServiceApi paramServiceApi;

    @Autowired
    ProductInfoServiceApi productInfoServiceApi;

    /**
     * 查询股权数量
     *
     * @param dto
     * @return
     */
    @DataSource(name = DataSource.DATASOURCE_COMMON)
    public StockResponse getStock(MemberAccountDto dto) {
        StockResponse response = new StockResponse();
        MemberAccountDto accountDto = memberAccountServiceApi.selectMemberAccountByCond(dto.getMemberId(), dto.getType());
        if (accountDto != null) {
            response.setBalance(accountDto.getBalance().intValue());
        } else {
            response.setBalance(0);
        }
        ParamBaseDto paramDto = paramServiceApi.queryByKey(CommonConstants.Param.MEMBER_ACCOUNT_STOCK);
        if (paramDto != null) {
            response.setRemark(paramDto.getParamValue());
        }
        return response;
    }

    /**
     * 查询股权记录列表
     *
     * @param dto
     * @return
     */
    public List<MemberStockResponse> getStockLogList(OrderProductDto dto) {
        List<MemberStockResponse> responses = new ArrayList<>();
        List<OrderProductDto> list = orderProductServiceApi.selectList(null, dto).getList();
        if (list != null) {
            for (OrderProductDto orderDto : list) {
                List<OrderProjectRecordDto> projects = orderProjectRecordServiceApi.selectOrderProjectRecordByOrderId(orderDto.getOrderId());
                if (projects != null) {
                    projects = DozerUtils.maps(projects, OrderProjectRecordDto.class);
                    for (OrderProjectRecordDto recordDto : projects) {
                        if (recordDto.getProjectType().equals(2)) {
                            MemberStockResponse response = new MemberStockResponse();
                            response.setBalance(recordDto.getCount());
                            response.setOrderId(recordDto.getOrderId());
                            response.setProductName(orderDto.getProductName());
                            response.setCreateTime(orderDto.getPayTime());
                            responses.add(response);
                        }
                    }
                }
            }
        }
        return responses;
    }

    /**
     * 查询股权记录详情
     *
     * @param orderId
     * @return
     */
    public MemberStockDetailResponse getStockDetail(Long orderId) {
        MemberStockDetailResponse response = new MemberStockDetailResponse();
        OrderProductDto productDto = orderProductServiceApi.findById(orderId);
        List<OrderProjectRecordDto> projects = orderProjectRecordServiceApi.selectOrderProjectRecordByOrderId(orderId);
        if (productDto != null) {
            ProductInfoDto productInfoDto = productInfoServiceApi.findById(productDto.getProductId());
            if (projects != null) {
                for (OrderProjectRecordDto recordDto : projects) {
                    if (recordDto.getProjectType().equals(2)) {
                        response.setBalance(recordDto.getCount());
                        response.setOrderId(recordDto.getOrderId());
                        response.setProductName(productInfoDto.getName());
                        response.setCreateTime(productDto.getPayTime());
                        response.setOffice(productInfoDto.getOffice());
                    }
                }
            }
        }
        return response;
    }
}
